﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace SkillEditor
{
    public class SkillEditorNodeWindow : EditorWindow
    {
        public static SkillEditorNodeWindow Open()
        {
            return GetWindow<SkillEditorNodeWindow>("Node");
        }

        public List<SkillNodeInfo> listNode;

        public GUIStyle style1;
        public GUIStyle style2;

        public void Awake()
        {
            listNode = new List<SkillNodeInfo>();

            //rootVisualElement.Add(new NodeVisualElement());
            style1 = new GUIStyle();
            style1.padding.left = 10;
            style1.padding.right = 10;
            style1.padding.bottom = 10;
            style1.padding.top = 10;

            style2 = new GUIStyle();
            style2.onNormal.background = new Texture2D(10, 10);
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    style2.onNormal.background.SetPixel(i, j, Color.green);
                }
            }
        }

        private Vector2 vt2;

        private void OnGUI()
        {
            EditorGUILayout.BeginVertical(style1);

            vt2 = EditorGUILayout.BeginScrollView(vt2, GUILayout.Height(600));

            for (int i = 0; i < listNode.Count; i++)
            {
                SkillNodeInfo skillNode = listNode[i];

                Rect rect = EditorGUILayout.BeginVertical();

                Debug.Log("1 " + rect);
                skillNode.name = EditorGUILayout.TextField("name:", skillNode.name);
                skillNode.type = EditorGUILayout.IntField("type:", skillNode.type);
                EditorGUILayout.LabelField("desc:");
                skillNode.desc = EditorGUILayout.TextArea(skillNode.desc, GUILayout.Height(50));
                GUILayout.Space(4);

                Rect rect2 = EditorGUILayout.BeginVertical(style2);
                //Debug.Log("1 " + rect2);
                GUI.Box(rect2, "参数");
                for (int j = 0; j < skillNode.listPort.Count; j++)
                {
                    SkillNodePortInfo skillNodePortInfo = skillNode.listPort[j];
                    skillNodePortInfo.name = EditorGUILayout.TextField("name:", skillNodePortInfo.name);
                    skillNodePortInfo.portType = EditorGUILayout.IntField("portType:", skillNodePortInfo.portType);
                    skillNodePortInfo.valueType = EditorGUILayout.TextField("valueType:", skillNodePortInfo.valueType);
                    GUILayout.Space(1);
                }

                if (GUILayout.Button("+"))
                {
                    skillNode.listPort.Add(new SkillNodePortInfo());
                }

                EditorGUILayout.EndVertical();

                EditorGUILayout.EndVertical();
                //GUILayout.Box("AA", GUILayout.Width(100), GUILayout.Height(50));
                Debug.Log("2 " + rect);

                GUILayout.BeginArea(rect, i + ".");
                GUILayout.EndArea();

                GUILayout.Space(12);
            }

            if (GUILayout.Button("新建"))
            {
                SkillNodeInfo skillNodeInfo = new SkillNodeInfo();
                listNode.Add(skillNodeInfo);
            }

            EditorGUILayout.EndScrollView();

            EditorGUILayout.EndVertical();
        }
    }

    public class SkillNodeInfo
    {
        public string name = "";
        public string desc = "";
        public int type = 0;
        public List<SkillNodePortInfo> listPort;

        public SkillNodeInfo()
        {
            listPort = new List<SkillNodePortInfo>();
        }
    }

    public class SkillNodePortInfo
    {
        public string name = "";
        public string valueType = "";
        public int portType;
    }
}

public class NodeVisualElement : VisualElement
{
    public NodeVisualElement()
    {
        style.flexGrow = 1;
        Create();
    }

    public void Create()
    {
        // Create some list of data, here simply numbers in interval [1, 1000]
        const int itemCount = 1000;
        var items = new List<string>(itemCount);
        for (int i = 1; i <= itemCount; i++)
            items.Add(i.ToString());

        // The "makeItem" function will be called as needed
        // when the ListView needs more items to render
        Func<VisualElement> makeItem = CreateItem;

        // As the user scrolls through the list, the ListView object
        // will recycle elements created by the "makeItem"
        // and invoke the "bindItem" callback to associate
        // the element with the matching data item (specified as an index in the list)
        Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items[i];

        ListView listView = new ListView();
        listView.style.flexGrow = 1;
        listView.itemHeight = 10;
        listView.makeItem = makeItem;
        listView.bindItem = bindItem;
        listView.itemsSource = items;
        listView.selectionType = SelectionType.Multiple;

        contentContainer.Add(listView);

        // Callback invoked when the user double clicks an item
        listView.onItemsChosen += Debug.Log;

        // Callback invoked when the user c
    }

    private VisualElement CreateItem()
    {
        VisualElement visualElement = new VisualElement();

        return visualElement;
    }
}